export default {
  mounted(el, binding) {
    el.oninput = () => {
      const num = binding.value   

      let reg = new RegExp('^(\\d+)\\.(\\d{'+num+'}).*$')
      let format = '$1.$2'

      if(num == 0){
        reg = new RegExp('^(\\d+).*$')
        format = '$1'
      }
      
      el.children[0].value = el.children[0].value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
      el.children[0].value = el.children[0].value.replace(/^\./g, ""); //验证第一个字符是数字
      el.children[0].value = el.children[0].value.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的"."
      el.children[0].value = el.children[0].value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
      el.children[0].value = el.children[0].value.replace(/^0[\d]{1,}/, '0'); // 零开头的只能一个0
      el.children[0].value = el.children[0].value.replace(reg, format);
      el.children[0].dispatchEvent(new Event('input'))
    }
  }
}